﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using store.Areas.Home.Models;

namespace store.Areas.Home.Controllers
{
    public class CategoryController : Controller
    {
        private readonly storeEntities _db = new storeEntities();
        //
        // GET: /Home/Category/

        public ActionResult Index(int id = 0)
        {
            try
            {
                List<Product> lists;
                int count;
                var from = 0;
                var to = 0;
                // neu co id thi show san pham theo id , khong co thi show het tat ca san pham
                //_db.Products.Where(x => x.UnitPrice >= from && x.UnitPrice <= to).ToList()
                if (id > 0)
                {
                    lists = _db.Products.Where(x => x.CategoryID == id).ToList();
                    //lay het product trong ca category chinh va phu neu co
                    var category = _db.Categories.Where(c => c.ParentID == id).ToList();
                    foreach (var item in category)
                    {
                        lists.AddRange(_db.Products.Where(x => x.CategoryID == item.CategoryID).ToList());
                    }
                    count = lists.Count();
                }
                else
                {
                    lists = _db.Products.ToList();
                    count = lists.Count();
                }
                //filter neu co
                if (HttpContext.Request["from"] != null && HttpContext.Request["to"] != null)
                {
                    from = int.Parse(HttpContext.Request["from"]);
                    to = int.Parse(HttpContext.Request["to"]);
                    if (id > 0)
                    {
                        lists = _db.Products.Where(x => x.CategoryID == id && x.UnitPrice >= from && x.UnitPrice <= to).ToList();
                        //lay het product trong ca category chinh va phu neu co
                        var category = _db.Categories.Where(c => c.ParentID == id).ToList();
                        foreach (var item in category)
                        {
                            lists.AddRange(_db.Products.Where(x => x.CategoryID == item.CategoryID && x.UnitPrice >= from && x.UnitPrice <= to).ToList());
                        }
                        count = lists.Count();
                    }
                    else
                    {
                        lists = _db.Products.Where(x => x.UnitPrice >= from && x.UnitPrice <= to).ToList();
                        count = lists.Count();
                    }
                }


                var pager = new Pager()
                {
                    TotalItemCount = count,
                    CurrentPage = 1,
                    PageSize = 9
                };
                if (!string.IsNullOrEmpty(HttpContext.Request.QueryString["page"]))
                {
                    pager.CurrentPage = int.Parse(HttpContext.Request.QueryString["page"]);
                }

                //lay ve tong so trang
                pager.PageCount = pager.TotalItemCount / pager.PageSize;
                //neu khong chia het thi + them 1 trang
                if (pager.TotalItemCount % pager.PageSize != 0)
                    pager.PageCount++;

                var html = "<div class='col-xs-8 col-sm-8'><ul class='pagination direction-nav'>";
                //neu la trang dau thi disabled nut previous
                if (pager.CurrentPage == 1)
                    html += "<li><a href='' class='btn disabled'><span class='icon-arrow-left'></span></a></li>";
                else
                {
                    int previous = pager.CurrentPage - 1;

                    //filter neu co
                    if (HttpContext.Request["from"] != null && HttpContext.Request["to"] != null)
                    {
                        html += string.Format("<li><a href='?page={0}&from={1}&to={2}' class='btn'><span class='icon-arrow-left'></span></a></li>", previous, from, to);
                    }
                    else
                    {
                        html += "<li><a href='?page=" + previous + "' class='btn'><span class='icon-arrow-left'></span></a></li>";
                    }
                }
                //active css trang hien tai
                for (var i = 1; i <= pager.PageCount; i++)
                {
                    if (pager.CurrentPage == i)
                    {
                        html += "<li class='active'><a href='javascript:void(0)'>" + i + "</a></li>";
                    }
                    else
                    {
                        if (HttpContext.Request["from"] != null && HttpContext.Request["to"] != null)
                        {
                            html += string.Format("<li><a href='?page={0}&from={1}&to={2}'>" + i + "</a></li>", i, from, to);
                        }
                        else
                        {
                            html += string.Format("<li><a href='?page={0}'>" + i + "</a></li>", i);
                        }

                    }
                }
                // neu la trang cua thi disabled nut next
                if (pager.CurrentPage == pager.PageCount)
                    html += "<li><a href='' class='btn disabled'><span class='icon-arrow-right'></span></a></li>";
                else
                {
                    int next = pager.CurrentPage + 1;

                    if (HttpContext.Request["from"] != null && HttpContext.Request["to"] != null)
                    {
                        html += string.Format("<li><a href='?page={0}&from={1}&to={2}' class='btn'><span class='icon-arrow-right'></span></a></li>", next, from, to);
                    }
                    else
                    {
                        html += "<li><a href='?page=" + next + "' class='btn'><span class='icon-arrow-right'></span></a></li>";
                    }

                }
                html += "</ul></div>";
                html += "<div class='col-xs-4 col-sm-4'><ul class='pagination direction-nav float-right'>";
                //neu la trang dau thi disabled nut previous
                if (pager.CurrentPage == 1)
                    html += "<li><a href='' class='btn disabled'><span class='icon-first-2'></span></a></li>";
                else
                {
                    if (HttpContext.Request["from"] != null && HttpContext.Request["to"] != null)
                    {
                        html += string.Format("<li><a href='?page={0}&from={1}&to={2}' class='btn'><span class='icon-first-2'></span></a></li>", 1, from, to);
                    }
                    else
                    {
                        html += "<li><a href='?page=1' class='btn'><span class='icon-first-2'></span></a></li>";
                    }

                }
                // neu la trang cua thi disabled nut next
                if (pager.CurrentPage == pager.PageCount)
                    html += "<li><a href='' class='btn disabled'><span class='icon-last-2'></span></a></li>";
                else
                {
                    if (HttpContext.Request["from"] != null && HttpContext.Request["to"] != null)
                    {
                        html += string.Format("<li><a href='?page={0}&from={1}&to={2}' class='btn'><span class='icon-last-2'></span></a></li>", pager.PageCount, from, to);
                    }
                    else
                    {
                        html += "<li><a href='?page=" + pager.PageCount + "' class='btn'><span class='icon-last-2'></span></a></li>";
                    }


                }
                html += "</ul></div>";
                ViewBag.HtmlPager = html;
                if (pager.PageCount < 2)
                    ViewBag.HtmlPager = "";


                var record = pager.PageSize;
                //neu tong so san pham khong chia deu cho so trang thuc hien lay range trang cuoi cung nhu sau
                if (pager.TotalItemCount < pager.CurrentPage * pager.PageSize)
                {
                    // (so ban ghi hien thi trong 1 trang - so ban ghi con thieu de chia het cho tung trang) = so ban ghi can hien thi cua trang cuoi
                    record = pager.PageSize - ((pager.CurrentPage * pager.PageSize) - pager.TotalItemCount);
                }

                //neu PageSize lon hon so ban ghi hien co thi...
                if (record > pager.TotalItemCount)
                {
                    record = pager.TotalItemCount;
                }

                return View(lists.GetRange(((pager.CurrentPage - 1) * pager.PageSize), record));
            }
            catch (Exception)
            {

                return HttpNotFound();
            }

        }

        public ActionResult Search(string keyword)
        {
            try
            {
                List<Product> lists = _db.Products.Where(x => x.ProductName.Contains(keyword)).ToList();
                int count = lists.Count();
                var pager = new Pager()
                {
                    TotalItemCount = count,
                    CurrentPage = 1,
                    PageSize = 9
                };
                if (!string.IsNullOrEmpty(HttpContext.Request.QueryString["page"]))
                {
                    pager.CurrentPage = int.Parse(HttpContext.Request.QueryString["page"]);
                }

                //lay ve tong so trang
                pager.PageCount = pager.TotalItemCount / pager.PageSize;
                //neu khong chia het thi + them 1 trang
                if (pager.TotalItemCount % pager.PageSize != 0)
                    pager.PageCount++;

                var html = "<div class='col-xs-8 col-sm-8'><ul class='pagination direction-nav'>";
                //neu la trang dau thi disabled nut previous
                if (pager.CurrentPage == 1)
                    html += "<li><a href='' class='btn disabled'><span class='icon-arrow-left'></span></a></li>";
                else
                {
                    int previous = pager.CurrentPage - 1;
                    html += "<li><a href='?keyword=" + keyword + "&page=" + previous + "' class='btn'><span class='icon-arrow-left'></span></a></li>";

                }
                //active css trang hien tai
                for (var i = 1; i <= pager.PageCount; i++)
                {
                    if (pager.CurrentPage == i)
                    {
                        html += "<li class='active'><a href='javascript:void(0)'>" + i + "</a></li>";
                    }
                    else
                    {

                        html += string.Format("<li><a href='?keyword={0}&page={1}'>" + i + "</a></li>", keyword, i);
                    }
                }
                // neu la trang cua thi disabled nut next
                if (pager.CurrentPage == pager.PageCount)
                    html += "<li><a href='' class='btn disabled'><span class='icon-arrow-right'></span></a></li>";
                else
                {
                    int next = pager.CurrentPage + 1;
                    html += "<li><a href='?keyword=" + keyword + "&page=" + next + "' class='btn'><span class='icon-arrow-right'></span></a></li>";

                }
                html += "</ul></div>";
                html += "<div class='col-xs-4 col-sm-4'><ul class='pagination direction-nav float-right'>";
                //neu la trang dau thi disabled nut previous
                if (pager.CurrentPage == 1)
                    html += "<li><a href='' class='btn disabled'><span class='icon-first-2'></span></a></li>";
                else
                {

                    html += "<li><a href='?keyword=" + keyword + "&page=1' class='btn'><span class='icon-first-2'></span></a></li>";

                }
                // neu la trang cua thi disabled nut next
                if (pager.CurrentPage == pager.PageCount)
                    html += "<li><a href='' class='btn disabled'><span class='icon-last-2'></span></a></li>";
                else
                {

                    html += "<li><a href='?keyword=" + keyword + "&page=" + pager.PageCount + "' class='btn'><span class='icon-last-2'></span></a></li>";

                }
                html += "</ul></div>";
                ViewBag.HtmlPager = html;
                if (pager.PageCount < 2)
                    ViewBag.HtmlPager = "";


                var record = pager.PageSize;
                //neu tong so san pham khong chia deu cho so trang thuc hien lay range trang cuoi cung nhu sau
                if (pager.TotalItemCount < pager.CurrentPage * pager.PageSize)
                {
                    // (so ban ghi hien thi trong 1 trang - so ban ghi con thieu de chia het cho tung trang) = so ban ghi can hien thi cua trang cuoi
                    record = pager.PageSize - ((pager.CurrentPage * pager.PageSize) - pager.TotalItemCount);
                }

                //neu PageSize lon hon so ban ghi hien co thi...
                if (record > pager.TotalItemCount)
                {
                    record = pager.TotalItemCount;
                }

                return View("Index", lists.GetRange(((pager.CurrentPage - 1) * pager.PageSize), record));
            }
            catch (Exception)
            {

                return HttpNotFound();
            }
        }

        [ChildActionOnly]
        public ActionResult Categorys()
        {
            return PartialView("_PartialCategory", _db.Categories.ToList());
        }

    }
}
